package com.honeyms.project.blog.blogcontent.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.honeyms.framework.aspectj.lang.annotation.Log;
import com.honeyms.framework.aspectj.lang.enums.BusinessType;
import com.honeyms.project.blog.blogcontent.domain.BlogContenAndDetail;
import com.honeyms.project.blog.blogcontent.domain.BlogContent;
import com.honeyms.project.blog.blogcontent.service.IBlogContentService;
import com.honeyms.project.blog.blogcontentdetail.domain.BlogContentDetail;
import com.honeyms.project.blog.blogcontentdetail.service.IBlogContentDetailService;
import com.honeyms.project.system.userdetail.domain.UserDetail;
import com.honeyms.project.system.userdetail.service.IUserDetailService;
import com.honeyms.framework.web.controller.BaseController;
import com.honeyms.framework.web.domain.AjaxResult;
import com.honeyms.common.utils.poi.ExcelUtil;
import com.honeyms.framework.web.page.TableDataInfo;

/**
 * 帖子内容Controller
 * 
 * @author honeyms
 * @date 2020-10-12
 */
@Controller
@RequestMapping("/blog/blogcontent")
public class BlogContentController extends BaseController
{
    private String prefix = "blog/blogcontent";

    @Autowired
    private IBlogContentService blogContentService;
    
    @Autowired
    private IBlogContentDetailService blogContentDetailService;
    
    @Autowired
    private IUserDetailService userDetailService;

    @RequiresPermissions("blog:blogcontent:view")
    @GetMapping()
    public String blogcontent()
    {
        return prefix + "/blogcontent";
    }

    /**
     * 查询帖子内容列表
     */
    @RequiresPermissions("blog:blogcontent:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BlogContent blogContent)
    {
        startPage("ishot asc, content_id desc");
        List<BlogContent> list = blogContentService.selectBlogContentList(blogContent);
        return getDataTable(list);
    }

    /**
     * 导出帖子内容列表
     */
    @RequiresPermissions("blog:blogcontent:export")
    @Log(title = "帖子内容", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BlogContent blogContent)
    {
        List<BlogContent> list = blogContentService.selectBlogContentList(blogContent);
        ExcelUtil<BlogContent> util = new ExcelUtil<BlogContent>(BlogContent.class);
        return util.exportExcel(list, "blogcontent");
    }

    /**
     * 新增帖子内容
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存帖子内容
     */
    @RequiresPermissions("blog:blogcontent:add")
    @Log(title = "帖子内容", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BlogContent blogContent)
    {
    	UserDetail userDetail = userDetailService.selectUserDetailById(getUserId());
    	if (userDetail.getIntegrate()<1 && getUserId()!=1) {
			return AjaxResult.error("你的积分不足,不能发布帖子!");
		}
    	if (blogContentService.insertBlogContent(blogContent)>0) {
    		if (getUserId()!=1) {
    			userDetail.setIntegrate(userDetail.getIntegrate()-1);
    			userDetailService.updateUserDetail(userDetail);
			}
		}
        return success();
    }

    /**
     * 修改帖子内容
     */
    @GetMapping("/edit/{contentId}")
    public String edit(@PathVariable("contentId") Long contentId, ModelMap mmap)
    {
        BlogContent blogContent = blogContentService.selectBlogContentById(contentId);
        BlogContentDetail blogContentDetail = blogContentDetailService.selectBlogContentDetailById(contentId);
        BlogContenAndDetail blogContenAndDetail = new BlogContenAndDetail();
        blogContenAndDetail.setBlogContent(blogContent);
        blogContenAndDetail.setBlogContentDetail(blogContentDetail);
        mmap.put("blogContent", blogContenAndDetail);
        return prefix + "/edit";
    }

    /**
     * 修改保存帖子内容
     */
    @RequiresPermissions("blog:blogcontent:edit")
    @Log(title = "帖子内容", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BlogContent blogContent,BlogContentDetail blogContentDetail)
    {
    	UserDetail userDetail = userDetailService.selectUserDetailById(getUserId());
    	if (userDetail.getIntegrate()<1 && getUserId()!=1) {
			return AjaxResult.error("你的积分不足,不能修改帖子!");
		}
    	if (blogContentService.updateBlogContent(blogContent,blogContentDetail)>0) {
			userDetail.setIntegrate(userDetail.getIntegrate()-1);
			userDetailService.updateUserDetail(userDetail);
		}
        return success();
    }

    /**
     * 删除帖子内容
     */
    @RequiresPermissions("blog:blogcontent:remove")
    @Log(title = "帖子内容", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(blogContentService.deleteBlogContentByIds(ids));
    }
}
